package com.jinzw.jform.rows

import android.content.Context
import android.support.v7.widget.AppCompatTextView
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast

import com.jinzw.jform.R
import com.jinzw.jform.core.DimensionUtils
import com.jinzw.jform.core.DimensionUtils.dpToPx
import com.jinzw.jform.core.ResultHandler

open class RightRow(context: Context) : AbstractTextRow<String>(context) {

    //will be created by onCreateView
    var dateView: TextView? = null


    var value: String? = null
        set(value) {
            field = value
            dateView?.text = value
        }
    var defaultDate: String? = null

    protected var datePicker: ((RightRow, ResultHandler<String>) -> Unit) = { dateRow, resultHandler ->
        Toast.makeText(context, "弹框", Toast.LENGTH_SHORT).show();
    }

    /**
     * Change this lambda to have a custom Date Picker (on click)
     */
    fun datePicker(block: (RightRow, ResultHandler<String>) -> Unit) {
        this.datePicker = block
    }

    protected var customizeDateView: ((RightRow, TextView) -> Unit) = { dateRow, textView ->
        textView.gravity = Gravity.RIGHT
        var dra = context.resources.getDrawable(R.drawable.you)
        dra.setBounds(0, 0, dra.getMinimumWidth(), dra.getMinimumHeight());
        textView.setCompoundDrawables(null, null, dra, null)


    }

    /**
     * Change this lambda to have a custom Date View
     */
    fun customizeDateView(block: (RightRow, TextView) -> Unit) {
        this.customizeDateView = block
    }

    init {
        validator = { v -> v != null }
        onCreateView<RightRow> {
            val layout = RelativeLayout(context)


            //Generated the DateView
            createDateView()
            val editTextLayoutParams = if (isEvenly) {
                RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dpToPx(evenlyWith))
            } else {
                RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            }
            editTextLayoutParams.leftMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_LEFT)
            editTextLayoutParams.topMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_TOP)
            editTextLayoutParams.bottomMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_BOTTOM)
            editTextLayoutParams.rightMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_RIGHT)
            editTextLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL)
            editTextLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
            dateView?.layoutParams = editTextLayoutParams
            dateView?.hint = defaultDate
            dateView?.gravity = Gravity.CENTER_VERTICAL
            dateView?.setTextSize(TypedValue.COMPLEX_UNIT_DIP, HINT_DEFAULT.toFloat())
            layout.addView(dateView)

            //Generated the Title
            createTitleView()
            val titleLayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            titleLayoutParams.leftMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_LEFT)
            titleLayoutParams.topMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_TOP)
            titleLayoutParams.bottomMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_BOTTOM)
            titleLayoutParams.rightMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_RIGHT)
            titleLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL)
            titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
            titleView?.layoutParams = titleLayoutParams
            layout.addView(titleView)

            layout
        }
        addOnViewCreatedListener<RightRow> {
            dateView?.setOnClickListener {
                onValueClicked()
            }
        }
    }

    /**
     * Use this lambda to change the visual aspect of the TitleView
     */
    protected fun createDateView(): TextView {
        dateView = AppCompatTextView(context)
        customizeDateView.invoke(this, this.dateView as TextView)
        return dateView as TextView
    }

    override fun onValueClicked() {
        val resultHandler = ResultHandler<String>()
        resultHandler.registerListener {
            value = it
        }
        datePicker.invoke(this, resultHandler)
    }

    override fun value(): String? {
        return value
    }
}